module.exports = {
    initialization:initialization
};
var logicUser = Logic.get('user');
function initialization(app){
    console.log('initialization post-user');

    app.post('/user/info', function(req, res) {
        if(req.session.isLogin !== true) {
            return res.send(ResCode.getCode(100));
        }
        return res.send(ResCode.getCode(0, req.session.info));
    });

    app.post('/user/loginOut', function(req, res) {
        if(req.session.isLogin === true) {
            logicUser.delLoginStatus(req);
            return res.send(ResCode.getCode(0));
        }
        return res.send(ResCode.getCode(0));
    });

    app.post('/user/login', function(req, res) {
        var data = {};
        data.account = req.body.account;
        data.password = req.body.password;

        if(!data.account || !data.password){
            return res.send(ResCode.getCode(1006));
        }
        DBOpt.User.login(data.account, data.password, function(resData){
            if(resData.code === 0){
                logicUser.setLoginStatus(req, resData.data);
            }
            return res.send(resData);
        });
    });

    app.post('/user/register', function(req, res){
        var data = {};
        data.userName = req.body.userName;
        data.password = req.body.password;
        data.account = req.body.account;

        if(!data.userName || !data.password || !data.account){
            return res.send(ResCode.getCode(1006));
        }
        DBOpt.User.register(data.account, data.userName, data.password, function(resData){
            if(resData.code === 0){
                logicUser.setLoginStatus(req, resData.data);
            }
            return res.send(resData);
        });
    });

    app.post('/user/update', function(req, res){
        var data = {};
        data.userName = req.body.userName;
        data.telphone = req.body.telphone;
        data.qq = req.body.qq;
        data.message = req.body.message;
        data.uid = req.body.uid;

        if(req.session.isLogin !== true) {
            return res.send(ResCode.getCode(100));
        }

        if(req.session.ID !== data.uid) {
            return res.send(ResCode.getCode(1006));
        }

        if(!data.userName && !data.telphone && !data.qq && !data.message){
            return res.send(ResCode.getCode(1006));
        }
        if(data.userName && (data.userName.length<2||data.userName.length>6)){
            return res.send(ResCode.getCode(1006));
        }
        if(data.telphone && data.telphone.length !== 11){
            return res.send(ResCode.getCode(1006));
        }
        if(data.qq && data.qq.length>14){
            return res.send(ResCode.getCode(1006));
        }
        if(data.message && data.message.length>200){
            return res.send(ResCode.getCode(1006));
        }
        DBOpt.User.update(data, function(resData){
            if(resData.code === 0){
                logicUser.setLoginStatus(req, resData.data);
            }
            return res.send(resData);
        });
    });
}